<?php
/**
 * Created by PhpStorm.
 * User: Airon
 * Date: 2016/11/17
 * Time: 17:21
 *
 */
namespace app\common\tools;

use think\Cache;
use think\Db;
use think\Exception;
use JPush\Client as JPushSdk;
class JPush
{
    public function push($title,$content,$user_uuid,$logicArray){
        $app_key="6c6d44086c92ab3a9d8bd658";
        $master_secret="535c34f89b42573a6ab0f6d7";

        $client = new JPushSdk($app_key, $master_secret);
        $alias = $user_uuid;
        // $alias = '8a5a03a6e4bc10b2312b32032b7ee96d';
        try{
            // $response = $client->push()->setPlatform(array('ios','android'))->addAlias($alias)
            // ->iosNotification($content,array(
            //     'extras'=>$logicArray
            // ))
            // ->
            // ->send();
  
            $response = $client->push()->setPlatform('all')->addAlias($alias)
            ->iosNotification($content,[
                'extras'=>$logicArray
            ])
            ->androidNotification($content,[
                'title' => $title,
                'builder_id' => 1,
                'extras' => $logicArray ]
            )->send();
            // dump($alias);
            // dump($response);die;
            return true;
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // dump($e->getMessage());die;
            return false;
//            return self::returnmsg(403,[],[],'Internal Server Error',$e,"推送失败~");
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // dump($e->getMessage());die;
            return false;
        }
    }

    public function push_test($title,$content,$user_uuid,$logicArray){
        $app_key="6c6d44086c92ab3a9d8bd658";
        $master_secret="535c34f89b42573a6ab0f6d7";

        $client = new JPushSdk($app_key, $master_secret);
        $alias = $user_uuid;
        // $alias = '8a5a03a6e4bc10b2312b32032b7ee96d';
        try{
            // $response = $client->push()->setPlatform(array('ios','android'))->addAlias($alias)
            // ->iosNotification($content,array(
            //     'extras'=>$logicArray
            // ))
            // ->
            // ->send();
  
            $response = $client->push()->setPlatform('all')->addAlias($alias)
            ->iosNotification($content,[
                'extras'=>$logicArray
            ])
            ->androidNotification($content,[
                'title' => $title,
                'builder_id' => 1,
                'extras' => $logicArray ]
            )->send();
            // dump($alias);
            // dump($response);die;
            return true;
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            // dump($e->getMessage());die;
            return $e->getMessage();
//            return self::returnmsg(403,[],[],'Internal Server Error',$e,"推送失败~");
        } catch (\JPush\Exceptions\APIRequestException $e) {
            // dump($e->getMessage());die;
            return $e->getMessage();
        }
    }
}